package com.fzu.mmdbproxyframework.mmdb.h2;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;




import com.fzu.mmdbproxyframework.context.ConfigContext;
import com.fzu.mmdbproxyframework.model.DBAbstract;
import com.fzu.mmdbproxyframework.model.DataSource;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;
import com.fzu.mmdbproxyframework.model.Table;

public class H2DB extends DBAbstract{
	private String Mode = "Embedded"; 
	private List<Table> tableList = new LinkedList<Table>();
	private List<String> tableNameList = new LinkedList<String>();
	public DataSource masterDataSource;
	public DataSource slaveDataSource;
	public List<DataSource> dataSourceList = new LinkedList<DataSource>();;
	public String DBNodeName;
	public String DBNodeType;
	public final String tcpPort = "9090";	
	public final String tcpPasswd = "sa";	
	public final String webPort = "8080";	
	public final String dbPath = "~/h2db/";	
	public final String DBName = "test";	
	public boolean active;
	public MMDataNodeCluster cluster;  
	
	
	/**
	 * 
	 */
	private H2DB() {
	}


	/**
	 * @param cluster
	 */
	public H2DB(MMDataNodeCluster cluster) {
		super();
		this.cluster = cluster;
	}


	public void TruncateTable() throws SQLException{
		for(Table table : getTableList()){
			getConnection().createStatement().execute(table.createTruncateTableStr());////清空mmdb中的表数据
			
		}
	}
	
	
	public java.sql.Connection getConnection() throws SQLException{
		//获取当前master的数据库链接
		java.sql.Connection conn= null;		
		conn=getMasterDataSource().getConnection();
//		System.out.println("获取当前master的数据库链接="+conn.hashCode());
		return conn;
	}
	

	

	public List<DataSource> getDataSourceList() {
		return dataSourceList;
	}

	public void setDataSourceList(List<DataSource> dataSourceList) {
		this.dataSourceList = dataSourceList;
	}
	
	
	public DataSource getMasterDataSource() {
//		System.out.println("dataSourceList.size()=="+dataSourceList.size());
//		System.out.println("masterDataSource==="+masterDataSource);
		for(DataSource dataSource :dataSourceList){
			if(dataSource.getType().equalsIgnoreCase("master")){
				setMasterDataSource(dataSource);
//				System.out.println("取出来的 masterDataSource="+masterDataSource.getDataSourcename());
			}
		}
//		if(masterDataSource == null){
//			System.out.println("h2 masterDataSource为空");
//			if(ConfigContext.getMaster_Slave().equalsIgnoreCase("true")){
////				System.out.println("dataSourceList.size()=="+dataSourceList.size());
//				for(DataSource dataSource :dataSourceList){
//					if(dataSource.getType().equalsIgnoreCase("master")){
//						setMasterDataSource(dataSource);
//						System.out.println("取出来的 masterDataSource="+masterDataSource.getDataSourcename());
//					}
//				}
//			}
//		}
		return masterDataSource;
	}
	private void setMasterDataSource(DataSource masterDataSource) {
		this.masterDataSource = masterDataSource;
	}
	public DataSource getSlaveDataSource() {
		if(masterDataSource == null){
			if(cluster.getConfigContext().getMaster_Slave().equalsIgnoreCase("true")){
				for(DataSource dataSource :dataSourceList){
					if(dataSource.getType().equalsIgnoreCase("slave")){
						slaveDataSource =dataSource;
					}
				}
			}
		}
		return slaveDataSource;
	}
	private void setSlaveDataSource(DataSource slaveDataSource) {
		this.slaveDataSource = slaveDataSource;
	}
	
	@Override
	public void setDBNodeType(String type) {
		DBNodeType = type;
	}


	public String getMode() {
		return Mode;
	}


	public void setMode(String mode) {
		Mode = mode;
	}


	public String getTcpPort() {
		return tcpPort;
	}


	public String getWebPort() {
		return webPort;
	}


	public String getDbPath() {
		return dbPath;
	}




	@Override
	public String getPort() {
		return tcpPort;
	}


	@Override
	public String getPasswd() {
		// TODO Auto-generated method stub
		return tcpPasswd;
	}


	public String getDBNodeName() {
		return DBNodeName;
	}


	public void setDBNodeName(String dBNodeName) {
		DBNodeName = dBNodeName;
	}

	public List<Table> getTableList() {
		if(tableList == null){
			setTableList(getMasterDataSource().getTableList());
		}
		return tableList;
	}


	public void setTableList(List<Table> tableList) {
		this.tableList = tableList;
	}


	public List<String> getTableNameList() {
		return tableNameList;
	}


	public void setTableNameList(List<String> tableNameList) {
		this.tableNameList = tableNameList;
	}


	public boolean isActive() {
		return active;
	}


	public void setActive(boolean active) {
		this.active = active;
	}


	@Override
	public String getDBName() {
		// TODO Auto-generated method stub
		return DBName;
	}


	public String getDBNodeType() {
		return DBNodeType;
	}


	public String getTcpPasswd() {
		return tcpPasswd;
	}
	
}
